package com.wll.design.观察者.springboot.event;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;

/**
 * @ClassName: EventListenerConfig.java
 * @author: wanglulu
 * @version: 1.0.0
 * @Description: 监听配置类
 *
 * 参考地址 :https://www.jianshu.com/p/873458c6c45a
 *
 * @createTime: 2021/11/25 15:58
 */
@Slf4j
@Configuration
public class EventListenerConfig {

    @EventListener
    public void handleEvent(Object event) {
        //监听所有事件 可以看看 系统各类时间 发布了哪些事件
        //可根据 instanceof 监听想要监听的事件
//        if(event instanceof CustomEvent) {
//
//        }
        log.info("all 事件 ****：{}", event);
    }

    @EventListener
    public void handleCustomEvent(CustomEvent customEvent) {
        //监听 CustomEvent事件
        log.info("监听到CustomEvent事件，消息为：{}, 发布时间：{}", customEvent.getMessageEntity(), customEvent.getTimestamp());
    }

    /**
     * 监听 code为wll的事件
     */
    @EventListener(condition = "#customEvent.messageEntity.code == 'wll'")
    public void handleCustomEventByCondition(CustomEvent customEvent) {
        //监听 CustomEvent事件
        log.info("监听到code为'wll'的CustomEvent事件，消息为：{}, 发布时间：{}", customEvent.getMessageEntity(), customEvent.getTimestamp());
    }

    @EventListener
    public void handleObjectEvent(MessageEntity messageEntity) {
        //这个和eventbus post方法一样了
        log.info("监听到对象事件，消息为：{}", messageEntity);

    }

}
